Block 3 Data Analysis

library(dplyr)
## Warning: package 'dplyr' was built under R version 3.3.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## Warning: package 'lubridate' was built under R version 3.3.3
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.3.3
library(plotly)
## Warning: package 'plotly' was built under R version 3.3.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggforce)
## Warning: package 'ggforce' was built under R version 3.3.3
library(sqldf)
## Warning: package 'sqldf' was built under R version 3.3.3
## Loading required package: gsubfn
## Warning: package 'gsubfn' was built under R version 3.3.3
## Loading required package: proto
## Warning: package 'proto' was built under R version 3.3.3
## Loading required package: RSQLite
## Warning: package 'RSQLite' was built under R version 3.3.3
library(tidyr)
## Warning: package 'tidyr' was built under R version 3.3.3
block3 <- read.csv("users_pw_anm_block3.csv", stringsAsFactors = FALSE, 
                   na.strings=c("NULL","NULL "," NULL ","NA"," ","", "-1"))
str(block3)
## 'data.frame':    3622 obs. of  95 variables:
##  $ user_id                           : chr  "238" "238" "238" "238" ...
##  $ user_type                         : chr  "1" "1" "1" "1" ...
##  $ user_name                         : chr  "Alka Santosh Nikam" "Alka Santosh Nikam" "Alka Santosh Nikam" "Alka Santosh Nikam" ...
##  $ user_sex                          : chr  "1" "1" "1" "1" ...
##  $ user_aadhar_id                    : num  7.3e+11 7.3e+11 7.3e+11 7.3e+11 7.3e+11 ...
##  $ user_login_pin                    : int  3165 3165 3165 3165 3165 3165 3165 NA 3165 3165 ...
##  $ user_mobile_no                    : num  9.88e+09 9.88e+09 9.88e+09 9.88e+09 9.88e+09 ...
##  $ block_id                          : int  3 3 3 3 3 3 3 NA 3 3 ...
##  $ center_id                         : int  54 54 54 54 54 54 54 NA 54 54 ...
##  $ subcenter_id                      : int  413 413 413 413 413 413 413 NA 413 413 ...
##  $ user_device_id                    : chr  "6e9d9846fe3c0c2e" "6e9d9846fe3c0c2e" "6e9d9846fe3c0c2e" "6e9d9846fe3c0c2e" ...
##  $ user_imei_no                      : chr  "353330066306915" "353330066306915" "353330066306915" "353330066306915" ...
##  $ registeration_date                : chr  "2017-02-18 11:42:18" "2017-02-18 11:42:18" "2017-02-18 11:42:18" "2017-02-18 11:42:18" ...
##  $ user_acc_status                   : int  1 1 1 1 1 1 1 NA 1 1 ...
##  $ fcm_registered_id                 : chr  "fA1j9H_V-rw:APA91bFLelJwkJDwvikESW6CPHy5PYYKk1GUmgj1Ldb-qv5PWzbG9IZxffIE7dqfk5i3CAnn6aehVtZ7hpTtttxXZEL7XjHo7kwaJnMyUYIPfHPeUZv"| __truncated__ "fA1j9H_V-rw:APA91bFLelJwkJDwvikESW6CPHy5PYYKk1GUmgj1Ldb-qv5PWzbG9IZxffIE7dqfk5i3CAnn6aehVtZ7hpTtttxXZEL7XjHo7kwaJnMyUYIPfHPeUZv"| __truncated__ "fA1j9H_V-rw:APA91bFLelJwkJDwvikESW6CPHy5PYYKk1GUmgj1Ldb-qv5PWzbG9IZxffIE7dqfk5i3CAnn6aehVtZ7hpTtttxXZEL7XjHo7kwaJnMyUYIPfHPeUZv"| __truncated__ "fA1j9H_V-rw:APA91bFLelJwkJDwvikESW6CPHy5PYYKk1GUmgj1Ldb-qv5PWzbG9IZxffIE7dqfk5i3CAnn6aehVtZ7hpTtttxXZEL7XjHo7kwaJnMyUYIPfHPeUZv"| __truncated__ ...
##  $ user_app_version                  : chr  "2.2.1" "2.2.1" "2.2.1" "2.2.1" ...
##  $ last_login_time                   : chr  "2017-09-30 16:21:07" "2017-09-30 16:21:07" "2017-09-30 16:21:07" "2017-09-30 16:21:07" ...
##  $ pw_profile_id                     : int  351 566 581 588 688 803 1147 NA 1148 1327 ...
##  $ qrcode                            : chr  "NK-DVL-LHR-2017-A-076" "NK-DVL-LHR-2017-A-077" "NK-DVL-LHR-2017-A-078" "NK-DVL-LHR-2017-A-079" ...
##  $ pw_profile_name                   : chr  "JYa Ganesh SonWNe" "Jayshri Manohar chuare" "Farat Liyakat Shekh" "Pratibha Dinesh Chadrima" ...
##  $ pw_profile_dob                    : chr  "0000-00-00" "0000-00-00" "0000-00-00" "0000-00-00" ...
##  $ pw_profile_age                    : chr  "26" "28" "26" "24" ...
##  $ pw_profile_address                : chr  "Deola (MalegaonRpad)" "Duulatnagar Deola" "Deola" "Deola" ...
##  $ subcenter_id.1                    : chr  "413" "413" "413" "413" ...
##  $ pw_profile_landmark               : chr  "walhari road" "Omnagar" "Hudaki" "Daulatnagar" ...
##  $ pw_profile_contact_no             : chr  "9096815085" "9765090997" "7741091259" "8007444491" ...
##  $ pw_profile_bpl_card               : chr  "1" "0" "0" "0" ...
##  $ pw_profile_edu                    : num  1 5 3 6 3 4 4 NA 2 3 ...
##  $ pw_profile_type_uid               : int  4 2 4 4 2 2 4 NA 2 4 ...
##  $ pw_profile_uid_no                 : num  NA 1.27e+11 NA NA 1.27e+11 ...
##  $ pw_profile_lmp                    : chr  "2016-07-06" "2016-08-21" "2016-10-25" "2016-07-11" ...
##  $ pw_profile_edd                    : chr  "2017-04-12" "2017-05-28" "2017-08-01" "2017-04-17" ...
##  $ pw_profile_first_preg             : chr  "0" "0" "0" "1" ...
##  $ pw_profile_birth_order            : chr  "2" "1" "2" NA ...
##  $ pw_profile_prev_pih               : int  0 0 0 NA NA NA NA NA 0 0 ...
##  $ pw_profile_prev_sb                : int  0 0 0 NA NA NA NA NA 0 0 ...
##  $ pw_profile_prev_low_wt            : int  0 0 0 NA NA NA NA NA 0 0 ...
##  $ pw_profile_prev_miscarriage       : int  0 0 0 NA NA NA NA NA 0 0 ...
##  $ pw_profile_prev_c_section         : int  0 1 0 NA NA NA NA NA 0 0 ...
##  $ pw_profile_prev_pph               : int  0 0 0 NA NA NA NA NA 0 0 ...
##  $ pw_profile_diabetes               : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_kidney_disease         : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_heart_disease          : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_thyroid_disease        : int  0 0 0 0 0 0 1 NA 0 0 ...
##  $ pw_profile_epilepsy               : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_reproductive_surg      : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_abdominal_surg         : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_other_flag             : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_other                  : chr  NA NA NA NA ...
##  $ pw_profile_family_diabetes        : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_family_pih             : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ pw_profile_multi_preg             : int  0 0 0 0 0 0 0 NA 9 0 ...
##  $ pw_profile_blood_group            : int  4 4 4 4 1 2 3 NA 2 2 ...
##  $ pw_profile_hiv                    : int  0 0 0 0 0 0 0 NA 9 0 ...
##  $ pw_profile_gps_lat                : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ pw_profile_gps_long               : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ pw_profile_form_id                : chr  "6e9d9846fe3c0c2e_2017-03-09 13:19:44_ver1.0.7" "6e9d9846fe3c0c2e_2017-03-13 14:10:34_ver1.0.7" "6e9d9846fe3c0c2e_2017-03-14 11:35:15_ver1.0.7" "6e9d9846fe3c0c2e_2017-03-14 11:53:46_ver1.0.7" ...
##  $ user_id.1                         : chr  "238" "238" "238" "238" ...
##  $ pw_profile_form_start_time        : chr  "2017-03-09 13:09:40" "2017-03-13 13:55:36" "2017-03-14 11:25:24" "2017-03-14 11:42:57" ...
##  $ pw_profile_form_fill_time         : chr  "2017-03-09 13:19:44" "2017-03-13 14:10:34" "2017-03-14 11:35:15" "2017-03-14 11:53:46" ...
##  $ pw_profile_reg_date               : chr  "2017-03-09 13:20:08" "2017-03-13 14:10:58" "2017-03-14 11:35:39" "2017-03-14 11:54:10" ...
##  $ pw_profile_fetal_demise           : chr  NA NA NA NA ...
##  $ pw_profile_fetal_demise_month     : chr  NA NA NA NA ...
##  $ anm_visit_detail_id               : int  362 616 631 638 738 855 1207 NA 1208 1412 ...
##  $ pw_profile_id.1                   : int  351 566 581 588 688 803 1147 NA 1148 1327 ...
##  $ qrcode.1                          : chr  "NK-DVL-LHR-2017-A-076" "NK-DVL-LHR-2017-A-077" "NK-DVL-LHR-2017-A-078" "NK-DVL-LHR-2017-A-079" ...
##  $ anm_visit_detail_preg_month       : chr  "9" "7" "5" "8" ...
##  $ anm_visit_detail_anc_count        : chr  "5" "4" "3" "4" ...
##  $ anm_visit_detail_headache         : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ anm_visit_detail_bp_sys           : int  110 110 120 120 115 98 130 NA 130 120 ...
##  $ anm_visit_detail_bp_dia           : int  70 70 80 80 78 66 90 NA 80 80 ...
##  $ anm_visit_detail_pulse            : int  83 74 72 80 72 72 74 NA 94 72 ...
##  $ anm_visit_detail_weight           : num  55 67 50 52 44 50 58 NA 59 37 ...
##  $ anm_visit_detail_height           : num  150 159 160 152 146 150 148 NA 158 152 ...
##  $ anm_visit_detail_bmi              : num  24.4 26.5 19.5 22.5 20.6 ...
##  $ anm_visit_detail_temp             : num  98 98 98 97 98 98 98 NA 98 97 ...
##  $ anm_visit_detail_hb               : num  9 9.5 11.5 9.4 10.5 10 10 NA 11 12 ...
##  $ anm_visit_detail_breathlessness   : num  0 0 0 0 0 0 0 NA 0 0 ...
##  $ anm_visit_detail_urine            : num  0 0 0 0 0 0 0 NA 0 0 ...
##  $ anm_visit_detail_abdomen_pain     : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ anm_visit_detail_edema            : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ anm_visit_detail_body_edema       : int  0 0 0 0 0 0 0 NA 0 0 ...
##  $ anm_visit_detail_fetal_bpm        : int  148 0 140 140 140 145 142 NA 0 140 ...
##  $ anm_visit_detail_gps_lat          : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ anm_visit_detail_gps_long         : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ anm_visit_detail_form_id          : chr  "6e9d9846fe3c0c2e_2017-03-09 13:19:44" "6e9d9846fe3c0c2e_2017-03-13 14:10:34" "6e9d9846fe3c0c2e_2017-03-14 11:35:15" "6e9d9846fe3c0c2e_2017-03-14 11:53:46" ...
##  $ user_id.2                         : chr  "238" "238" "238" "238" ...
##  $ anm_visit_detail_form_start_time  : chr  "2017-03-09 13:09:40" "2017-03-13 13:55:36" "2017-03-14 11:25:24" "2017-03-14 11:42:57" ...
##  $ anm_visit_detail_form_fill_time   : chr  "2017-03-09 13:19:44" "2017-03-13 14:10:34" "2017-03-14 11:35:15" "2017-03-14 11:53:46" ...
##  $ anm_visit_detail_followup_date    : chr  "2017-03-28" "2017-04-11" "2017-04-11" "2017-04-04" ...
##  $ anm_visit_detail_receive_time     : chr  "2017-03-09 13:20:08" "2017-03-13 14:10:58" "2017-03-14 11:35:39" "2017-03-14 11:54:10" ...
##  $ system_risk_reason                : chr  NA "PREVIOUS C-SECTION" NA NA ...
##  $ risk_status                       : chr  "1" "3" "1" "1" ...
##  $ first_record                      : chr  "1" "1" "1" "1" ...
##  $ anm_visit_detail_edd_by_sonography: chr  NA NA NA NA ...
block3$user_id <- as.numeric(block3$user_id)
## Warning: NAs introduced by coercion
#Following variables represent categories and not character or intgers
categorical <- c("user_type", "user_sex", "user_acc_status", "pw_profile_bpl_card", "pw_profile_edu", "pw_profile_type_uid",
                 "pw_profile_first_preg", "pw_profile_birth_order", "pw_profile_prev_pih",
                 "pw_profile_prev_sb", "pw_profile_prev_low_wt", "pw_profile_prev_miscarriage",
                 "pw_profile_prev_c_section", "pw_profile_prev_pph", "pw_profile_heart_disease",
                 "pw_profile_thyroid_disease", "pw_profile_epilepsy", "pw_profile_reproductive_surg",
                 "pw_profile_other_flag", "pw_profile_family_diabetes", "pw_profile_family_pih",
                 "pw_profile_multi_preg", "pw_profile_blood_group", "pw_profile_hiv")

#Converting to categorical variables
block3[, categorical] <- lapply(block3[, categorical], as.factor)
str(block3[, categorical])
## 'data.frame':    3622 obs. of  24 variables:
##  $ user_type                   : Factor w/ 19 levels " ANAEMIA"," HIGH BLOOD PRESSURE",..: 5 5 5 5 5 5 5 NA 5 5 ...
##  $ user_sex                    : Factor w/ 4 levels " ANAEMIA","0000-00-00",..: 3 3 3 3 3 3 3 NA 3 3 ...
##  $ user_acc_status             : Factor w/ 1 level "1": 1 1 1 1 1 1 1 NA 1 1 ...
##  $ pw_profile_bpl_card         : Factor w/ 8 levels "0","1","7057668856",..: 2 1 1 1 1 1 1 NA 1 2 ...
##  $ pw_profile_edu              : Factor w/ 8 levels "0","1","2","3",..: 2 6 4 7 4 5 5 NA 3 4 ...
##  $ pw_profile_type_uid         : Factor w/ 5 levels "1","2","3","4",..: 4 2 4 4 2 2 4 NA 2 4 ...
##  $ pw_profile_first_preg       : Factor w/ 8 levels "0","1","2017-01-09",..: 1 1 1 2 2 2 2 NA 1 1 ...
##  $ pw_profile_birth_order      : Factor w/ 8 levels "0","1","2","2017-10-16",..: 3 2 3 NA NA NA NA NA 2 2 ...
##  $ pw_profile_prev_pih         : Factor w/ 4 levels "0","1","2","7": 1 1 1 NA NA NA NA NA 1 1 ...
##  $ pw_profile_prev_sb          : Factor w/ 3 levels "0","1","7": 1 1 1 NA NA NA NA NA 1 1 ...
##  $ pw_profile_prev_low_wt      : Factor w/ 3 levels "0","1","7": 1 1 1 NA NA NA NA NA 1 1 ...
##  $ pw_profile_prev_miscarriage : Factor w/ 3 levels "0","1","7": 1 1 1 NA NA NA NA NA 1 1 ...
##  $ pw_profile_prev_c_section   : Factor w/ 3 levels "0","1","7": 1 2 1 NA NA NA NA NA 1 1 ...
##  $ pw_profile_prev_pph         : Factor w/ 3 levels "0","1","7": 1 1 1 NA NA NA NA NA 1 1 ...
##  $ pw_profile_heart_disease    : Factor w/ 2 levels "0","9": 1 1 1 1 1 1 1 NA 1 1 ...
##  $ pw_profile_thyroid_disease  : Factor w/ 4 levels "0","1","7","9": 1 1 1 1 1 1 2 NA 1 1 ...
##  $ pw_profile_epilepsy         : Factor w/ 4 levels "0","1","7","9": 1 1 1 1 1 1 1 NA 1 1 ...
##  $ pw_profile_reproductive_surg: Factor w/ 3 levels "0","1","9": 1 1 1 1 1 1 1 NA 1 1 ...
##  $ pw_profile_other_flag       : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 NA 1 1 ...
##  $ pw_profile_family_diabetes  : Factor w/ 3 levels "0","1","9": 1 1 1 1 1 1 1 NA 1 1 ...
##  $ pw_profile_family_pih       : Factor w/ 3 levels "0","1","9": 1 1 1 1 1 1 1 NA 1 1 ...
##  $ pw_profile_multi_preg       : Factor w/ 5 levels "0","2","3","7",..: 1 1 1 1 1 1 1 NA 5 1 ...
##  $ pw_profile_blood_group      : Factor w/ 10 levels "0","1","2","3",..: 5 5 5 5 2 3 4 NA 3 3 ...
##  $ pw_profile_hiv              : Factor w/ 6 levels "0","1","2","4",..: 1 1 1 1 1 1 1 NA 6 1 ...
#Following are date variables and should be changed to date
datevars <- c("pw_profile_lmp", "pw_profile_edd")

block3[, datevars] <- lapply(block3[, datevars], ymd)
## Warning: 8 failed to parse.
## Warning: 2 failed to parse.
str(block3[, datevars])
## 'data.frame':    3622 obs. of  2 variables:
##  $ pw_profile_lmp: Date, format: "2016-07-06" "2016-08-21" ...
##  $ pw_profile_edd: Date, format: "2017-04-12" "2017-05-28" ...
block3$pw_profile_dob <- dmy(block3$pw_profile_dob)
## Warning: All formats failed to parse. No formats found.
#Following are datetime variables and should be changed to datetime
datetimevars <- c("registeration_date", "last_login_time", "pw_profile_form_start_time", "pw_profile_form_fill_time",
                  "pw_profile_reg_date")

block3[, datetimevars] <- lapply(block3[, datetimevars], ymd_hms)
## Warning: 8 failed to parse.

## Warning: 2 failed to parse.
str(block3[, datetimevars])
## 'data.frame':    3622 obs. of  5 variables:
##  $ registeration_date        : POSIXct, format: "2017-02-18 11:42:18" "2017-02-18 11:42:18" ...
##  $ last_login_time           : POSIXct, format: "2017-09-30 16:21:07" "2017-09-30 16:21:07" ...
##  $ pw_profile_form_start_time: POSIXct, format: "2017-03-09 13:09:40" "2017-03-13 13:55:36" ...
##  $ pw_profile_form_fill_time : POSIXct, format: "2017-03-09 13:19:44" "2017-03-13 14:10:34" ...
##  $ pw_profile_reg_date       : POSIXct, format: "2017-03-09 13:20:08" "2017-03-13 14:10:58" ...
#Reading case closed files
pw_case_flag <- read.csv("pw_case_flag_05_apr.csv", stringsAsFactors = FALSE, header = TRUE,
                         na.strings=c("NULL","NULL "," NULL ","NA"," ","", "-1"))

#Select profiles whose cases are closed 
pw_closed <- sqldf("select distinct(pw_profile_id) from pw_case_flag where pw_case_flag_closed = 1")

#Removing closed profiles from block3
block3_1 <- sqldf("select * from block3 where pw_profile_id not in pw_closed")

#Removing user ids less than 10
block3_1 <- block3_1[(which(block3_1$user_id > 10)),]
agg_user <- block3_1 %>% group_by(user_id) %>% tally()

usr_lines <- ggplot(agg_user, aes(x = user_id, y = n)) + 
                 geom_line() + xlab("User Id") + ylab("Number of Records")

ggplotly(usr_lines)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
#Aggregate number of records by each user
usr_points <- ggplot(agg_user, aes(x = user_id, y = n)) + 
                  geom_point() + xlab("User Id") + ylab("Number of Records")

ggplotly(usr_points)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
block3_1$anm_visit_detail_form_fill_date <- as.Date(block3_1$anm_visit_detail_form_fill_time)

#Form filling trend by each user
agg_user_date <- block3_1 %>% group_by(user_id, anm_visit_detail_form_fill_date) %>% tally()

agg_user_date <- agg_user_date[!is.na(agg_user_date$anm_visit_detail_form_fill_date),]

usrid <- sort(unique(na.omit(agg_user_date$user_id)))

#Show form filling trend for each of the users
for(i in seq_len(length(usrid))){
  plot <- ggplot(agg_user_date, aes(x = anm_visit_detail_form_fill_date, y = n)) +
    geom_point() + xlab("Date") + ylab("Number of records") +
    ggtitle(paste("User id", as.character(usrid[i]))) +
    facet_wrap_paginate(~user_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
  #print(ggplotly(plot))
  print(plot)
}

#Aggregate values by each subcenter
agg_subcent <- block3_1 %>% group_by(subcenter_id) 

agg_subcent_pw <- agg_subcent %>% group_by(pw_profile_id) 

pw_profiles <- sort(unique(na.omit(agg_subcent_pw$pw_profile_id)))

subcent <- sort(unique(na.omit(agg_subcent$subcenter_id)))

#Show systolic bp values in each of the subcenters
#for(i in seq_len(length(pw_profiles))){
for(i in seq_len(1:10)){
  plot <- ggplot(agg_subcent_pw, aes(x = anm_visit_detail_anc_count, y = anm_visit_detail_bp_sys)) +
    geom_point() + xlab("ANM visit number") + ylab("Blood Pressure Systolic") +
    ggtitle(paste("PW Profile", as.character(pw_profiles[i])))
    #facet_wrap_paginate(~pw_profile_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
  print(plot)
}
## Warning in seq_len(1:10): first element used of 'length.out' argument

ggplot(agg_subcent_pw[agg_subcent_pw$pw_profile_id == 6808,], aes(x = anm_visit_detail_anc_count, y = anm_visit_detail_bp_sys)) +
  geom_point() + xlab("ANM visit number") + ylab("Blood Pressure Systolic")

#Show diastolic bp values in each of the subcenters
for(i in seq_len(length(subcent))){
plot <- ggplot(agg_subcent, aes(x = pw_profile_id, y = anm_visit_detail_bp_dia))         + geom_point() + xlab("Pw Profile Id") + ylab("Blood Pressure Diastolic") +
        ggtitle(paste("Subcenter", as.character(subcent[i]))) +
        facet_wrap_paginate(~subcenter_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
print(plot)
}

#Show pulse values in each of the subcenters
 for(i in seq_len(length(subcent))){
   plot <- ggplot(agg_subcent, aes(x = pw_profile_id, y = anm_visit_detail_pulse)) +
     geom_point() + xlab("Pw Profile Id") + ylab("Pulse rate") +
     ggtitle(paste("Subcenter", as.character(subcent[i]))) +
     facet_wrap_paginate(~subcenter_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
  print(plot)
}

#Show weight values in each of the subcenters
 for(i in seq_len(length(subcent))){
   plot <- ggplot(agg_subcent, aes(x = pw_profile_id, y = anm_visit_detail_weight)) +
     geom_point() + xlab("Pw Profile Id") + ylab("Weight") +
     ggtitle(paste("Subcenter", as.character(subcent[i]))) +
     facet_wrap_paginate(~subcenter_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
   print(plot)
 }

#Show height values in each of the subcenters
 for(i in seq_len(length(subcent))){
   plot <- ggplot(agg_subcent, aes(x = pw_profile_id, y = anm_visit_detail_height)) +
     geom_point() + xlab("Pw Profile Id") + ylab("Height") +
     ggtitle(paste("Subcenter", as.character(subcent[i]))) +
     facet_wrap_paginate(~subcenter_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
   print(plot)
 }

#Show bmi values in each of the subcenters
for(i in seq_len(length(subcent))){
   plot <- ggplot(agg_subcent, aes(x = pw_profile_id, y = anm_visit_detail_bmi)) +
     geom_point() + xlab("Pw Profile Id") + ylab("BMI") +
     ggtitle(paste("Subcenter", as.character(subcent[i]))) +
     facet_wrap_paginate(~subcenter_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
   print(plot)
}

#Show temperature values in each of the subcenters
for(i in seq_len(length(subcent))){
   plot <- ggplot(agg_subcent, aes(x = pw_profile_id, y = anm_visit_detail_temp)) +
     geom_point() + xlab("Pw Profile Id") + ylab("Temperature") +
     ggtitle(paste("Subcenter", as.character(subcent[i]))) +
     facet_wrap_paginate(~subcenter_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
 print(plot)
}

#Show Haemoglobin count values in each of the subcenters
 for(i in seq_len(length(subcent))){
   plot <- ggplot(agg_subcent, aes(x = pw_profile_id, y = anm_visit_detail_hb)) +
     geom_point() + xlab("Pw Profile Id") + ylab("Hb count")  +
     ggtitle(paste("Subcenter", as.character(subcent[i]))) +
     facet_wrap_paginate(~subcenter_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
   print(plot)
}

#Show Fetal bpm values in each of the subcenters
for(i in seq_len(length(subcent))){
   plot <- ggplot(agg_subcent, aes(x = pw_profile_id, y = anm_visit_detail_fetal_bpm)) +
     geom_point() + xlab("Pw Profile Id") + ylab("Fetal bpm") +
     ggtitle(paste("Subcenter", as.character(subcent[i]))) +
     facet_wrap_paginate(~subcenter_id,nrow = 1, ncol = 1, scales = "fixed", page = i)
   print(plot)
}

#-----Center wise records gainst target number of records------------------
target_anc <- read.csv("Deola_block3_target.csv", skip = 1, header = TRUE, stringsAsFactors = FALSE, 
                       na.strings=c("NULL","NULL "," NULL ","NA"," ","", "-1"))

subcenter_details <- read.csv("subcenter.csv", header = TRUE, stringsAsFactors = FALSE, 
                              na.strings=c("NULL","NULL "," NULL ","NA"," ","", "-1"))

target_anc1 <- target_anc %>% left_join(subcenter_details, by = c("Sub.Center.Name"="subcenter_name"))

total_subcent <- agg_subcent %>% tally()

total_subcent1 <- merge(total_subcent, target_anc1, by = "subcenter_id", all.x = TRUE)

ggplot(total_subcent1, aes(x = subcenter_id, y = n)) +
  geom_bar(stat = "identity") + xlab("Subcenter Id") + ylab("Number of Records") +
  ggtitle("Subcenter-wise record collection") +
  geom_line(aes(y = Expected.ANCs.in.a.year))

total_subcent2 <- total_subcent1 %>% gather(key = record, value = count, n, Expected.ANCs.in.a.year)
  
ggplot(total_subcent2, aes(x = subcenter_id, y = count, fill = record)) +
  geom_bar(stat = "identity", position = "dodge") + xlab("Subcenter Id") + ylab("Number of Records") +
  ggtitle("Subcenter-wise record collection") 

#--------------Fetal bpm values-----------------------
fhr <- read.csv("fhr_limits.csv",header = TRUE, stringsAsFactors = FALSE, 
                na.strings=c("NULL","NULL "," NULL ","NA"," ","", "-1"))

agg_subcent_pw$anm_visit_detail_preg_month <- as.numeric(agg_subcent_pw$anm_visit_detail_preg_month)
## Warning: NAs introduced by coercion
agg_subcent_pw1 <- merge(agg_subcent_pw, fhr, by.x = "anm_visit_detail_preg_month", by.y = "month", all.x = TRUE) %>%
                     group_by(pw_profile_id) %>% arrange(pw_profile_id, anm_visit_detail_preg_month)
## Warning: package 'bindrcpp' was built under R version 3.3.3
agg_subcent_pw2 <- agg_subcent_pw1 %>% gather(key = record, value = count, bpm_low, bpm_high)


ggplot(agg_subcent_pw2[agg_subcent_pw2$pw_profile_id == 6084,], aes(x = anm_visit_detail_preg_month, y = anm_visit_detail_fetal_bpm)) +
  geom_point(na.rm = TRUE) + xlab("Pregnancy month") + ylab("Fetal Heart Rate") +
  geom_line(aes(y = count, colour = record)) +
  ggtitle("Fetal Heart Rate for PW Profile 6084")